/*
 * Copyright (C) 2017-2025
 * ZKMall All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.zkmall.admin.service.word.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.zkmall.admin.dao.word.CerePlatformWordDAO;
import com.shop.zkmall.admin.param.word.WordParam;
import com.shop.zkmall.admin.service.log.CerePlatformLogService;
import com.shop.zkmall.admin.service.word.CerePlatformWordService;
import com.shop.zkmall.commons.domain.common.Page;
import com.shop.zkmall.commons.domain.user.CerePlatformUser;
import com.shop.zkmall.commons.domain.word.CerePlatformWord;
import com.shop.zkmall.commons.exception.CoBusinessException;
import com.shop.zkmall.commons.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zkmall zkmallvip@163.com
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CerePlatformWordServiceImpl implements CerePlatformWordService {

    private final CerePlatformWordDAO cerePlatformWordDAO;

    private final CerePlatformLogService cerePlatformLogService;

    @Override
    public CerePlatformWord checkWord(String keyWord, Long wordId) {
        return cerePlatformWordDAO.checkWord(keyWord, wordId);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void save(CerePlatformWord word, CerePlatformUser user) {
        String time = TimeUtils.yyMMddHHmmss();
        word.setCreateTime(time);
        cerePlatformWordDAO.insert(word);
        //新增日志
        cerePlatformLogService.addLog(user, "评论管理", "平台端操作", "添加关键词", String.valueOf(word.getWordId()), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void update(CerePlatformWord word, CerePlatformUser user) {
        String time = TimeUtils.yyMMddHHmmss();
        word.setUpdateTime(time);
        cerePlatformWordDAO.updateByPrimaryKeySelective(word);
        //新增日志
        cerePlatformLogService.addLog(user, "评论管理", "平台端操作", "修改关键词", String.valueOf(word.getWordId()), time);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void delete(CerePlatformWord word, CerePlatformUser user) {
        cerePlatformWordDAO.deleteByPrimaryKey(word.getWordId());
        //新增日志
        cerePlatformLogService.addLog(user, "评论管理", "平台端操作", "删除关键词", String.valueOf(word.getWordId()), TimeUtils.yyMMddHHmmss());
    }

    @Override
    public CerePlatformWord getById(Long wordId) {
        return cerePlatformWordDAO.selectByPrimaryKey(wordId);
    }

    @Override
    public Page<CerePlatformWord> getAll(WordParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<CerePlatformWord> list = cerePlatformWordDAO.getAll(param);
        PageInfo<CerePlatformWord> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void start(Integer state) {
        cerePlatformWordDAO.updateState(state);
    }
}
